<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>before</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
</head>

<body>
  <button id="myButton">点击</button>
  <script>
    // _.before(n, func)
    // 调用次数不超过 n 次。 之后再调用这个函数，将返回一次最后调用func的结果
    const button = document.getElementById('myButton');
    let count = 0;
    // 参数为3，就是不能超过3次，也就是只有两次机会，他只会调用两次
    let beforeFunc = _.before(3, () => {
      count++;
      console.log("Function called 3 times!", count);
    });
    button.addEventListener('click', beforeFunc);
  </script>
</body>

</html>